package proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by zuosh on 2016/8/5 0005.
 */
public class DynamicProxyUseCase {

    interface Ihello {
        void sayHello();
    }

    static class Hello implements Ihello {
        @Override
        public void sayHello() {
            System.out.println("hello,thx");
        }
    }

    static class SayhelloHandler implements InvocationHandler {
        private Object origObject;


        Object bind(Object object) {
            this.origObject = object;
            return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("welcome:\n");
            return method.invoke(origObject, args);
        }
    }

    public static void main(String[] args) {
        Ihello hello = (Ihello) new SayhelloHandler().bind(new Hello());
        hello.sayHello();
        System.out.println(hello.toString());
    }
}
